<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>活动管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container" id="app">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="table-search">
                    <div class="layui-inline">
                        <label class="layui-form-label">选择区服</label>
                        <div class="layui-input-inline" style="width: 200px">
                            <div id="gameArea"></div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">活动类型</label>
                        <div class="layui-input-inline">
                            <input type="text" name="acttype" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">活动ID</label>
                        <div class="layui-input-inline">
                            <input type="text" name="actid" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline">
                        <label class="layui-form-label">角色ID</label>
                        <div class="layui-input-inline">
                            <input type="comment" name="roleid" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-inline" >
                        <label class="layui-form-label">活动时间</label>
                        <div class="layui-input-inline">
                            <input type="text" name="createDateBegin" id="createTime0"
                                   placeholder="开始时间" autocomplete="off" class="layui-input" />
                            <input type="text" name="createDateEnd" id="createTime1"
                                   placeholder="截止时间" autocomplete="off" class="layui-input" />
                        </div>
                    </div>



                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 统计</button>
                            <button type="submit1" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn1"><i class="layui-icon"></i> 发送结果查询</button>
                        </div>

                </form>

            </div>

        </fieldset>
    </div>
</div>

<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm" auth="payOrder:export" lay-event="send">发送</button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" auth="payOrder:export" lay-event="sendselect">按照查询结果发送</button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" auth="payOrder:export" lay-event="export">导出</button>
    </div>
</script>
<script type="text/html" id="currentTableBar">
</script>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/common.js?v=5" charset="utf-8"></script>
<script src="/xm-select-v1.2.4/xm-select.js"></script>

<script>
    var form,$,_this;
    var services = JSON.parse(localStorage.getItem('services'))

    new Vue({
        el: "#app",
        data: {
            services:services,
        },
        methods:{

        },
        created:function(){
            _this = this;
        },
        mounted:function () {
            layui.use(['form', 'table','laydate'], function () {
                form = layui.form;
                $ = layui.jquery;
                var isLodedTable = false;

                var table = layui.table;
                var laydate=layui.laydate;

                var data = [];
                for(var k in services){
                    var v = services[k];
                    data.push({
                        name:v.name,
                        value:v.server_id,
                    })
                }

                laydate.render({
                    elem: '#createTime0',
                    type:'datetime',
                    value: getYmd(0)+ " 00:00:00"
                });
                laydate.render({
                    elem: '#createTime1',
                    type:'datetime',
                    value: getYmd(1) + " 00:00:00"
                });
                xmSelect.render({
                    el: '#gameArea',
                    name:"gameArea",
                    language: 'zn',
                    data: data
                });
                var cols = [
                    [
                        { title: '区服', field: "severarea" },
                        { title: '角色ID', field: "roleId" },
                        { title: '金额', field: "totalAmount" },
                    ]
                ];
                var  operatecols = [
                    [
                        { title: '日期', field: "created_at" },
                        { title: '角色ID', field: "RoleId" },
                        { title: '活动ID', field: "ActId" },
                        { title: '活动类型', field: "ActType" },
                        { title: '金额', field: "AccuRcYmb" },
                        { title: '推送地址', field: "pushUrl" },
                        { title: '发送状态', field: "status" ,templet:function(row){
                                // 例如:
                                console.log("11222"+row.status)
                                if (row.status == 0) {
                                    return '成功';
                                } else if (row.status == 1) {
                                    return '失败';
                                }else if (row.status == 3) {
                                    return '未找到角色';
                                }else if (row.status == 4) {
                                    return '操作正在处理';
                                }
                                else if (row.status == 1000) {
                                    return '无效的JSON参数';
                                }
                                else {
                                    return '未知状态';
                                }
                            }},
                    ]
                ];
                var originalData; // 原始数据
                var senddata;
                var loadTable = function() {


                    table.render({
                        where: form.val('table-search'),
                        elem: '#currentTableId',
                        url: '/admin/ActEdit/paginate',
                        defaultToolbar: ['filter', 'print'],
                        width: 2300,
                        toolbar: '#toolbarDemo',
                        cols: cols,
                        limits: [10, 15, 20, 25, 50, 100],
                        limit: 10,
                        page: true,
                        skin: 'line',
                        data:cols
                    });

                    isLodedTable = true;
                };

                // 初始加载列表
                loadTable();// 监听搜索操作
                //查询结果
                form.on('submit(data-search-btn1)', function(data) {
                    // 更新列表的列定义
                    //执行搜索重载
                    table.reload('currentTableId', {
                        url: '/admin/ActEdit/operatepaginate', //
                        page: {
                            curr: 1
                        },
                        cols:operatecols
                        , where: form.val('table-search')
                    }, 'data');

                    return false;
                });
                form.render()
                // 监听搜索操作
                form.on('submit(data-search-btn)', function (data) {
                    if (!isLodedTable){
                        loadTable();
                    } else {
                        //执行搜索重载
                        table.reload('currentTableId', {
                            url: '/admin/ActEdit/paginate',
                            page: {
                                curr: 1
                            },
                            cols:cols
                            , where: form.val('table-search'),
                            parseData: function(res) {
                                senddata=res.data;
                            }
                        }, 'data');
                    }

                    return false;
                });




                // 监听按钮点击事件
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'export') {
                        var searchData = form.val('table-search');
                        searchData.page = 1;
                        searchData.limit = 9999999;
                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        //layer.close(layerIndex)
                        $.get('/admin/payOrder/export',searchData,function(res){
                            layer.close(layerIndex)
                            browserDownload( "/" + res.data);
                        });
                    }
                });

                // 监听按钮点击事件 (发送请求)
                table.on('toolbar(currentTableFilter)', function (obj) {
                    if (obj.event === 'send') {
                        var searchData = form.val('table-search');

                        searchData.data=senddata;
                        console.log('snda '+senddata[0].status)
                        if (senddata[0].status ) {
                            layer.msg("请重新查询之后在发送...");
                            return false;
                        }
                        //判断roleid是否有重复
                        if (!senddata ) {
                            layer.msg("请重新查询之后在发送...");
                            return false;
                        }
                        var roleIds = senddata.map(function(obj) {
                            return obj.roleId;
                        });
                        var uniqueRoleIds = [...new Set(roleIds)];

                        // 判断是否存在重复的roleId
                        if (roleIds.length !== uniqueRoleIds.length) {
                            layer.msg("存在重复的ID...");
                            return false;
                        }
                        if(!searchData.acttype){

                            layer.msg("角色类型不能为空...");

                            return false;
                        }
                        if(!searchData.actid){
                            layer.msg("角色ID不能为空...");
                            return false;
                        }
                        if(!searchData.data){
                            layer.msg("查询数据为空,请查询之后在执行发送...");
                            return false;
                        }

                        console.log('查询数据xxxx'+senddata)

                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        $.post('/admin/ActEdit/send',searchData,function(res){
                            layer.close(layerIndex)
                            //loadTable();// 监听搜索操作
                            //重新加载操作记录
                            // table.reload('currentTableId', {
                            //     url: '/admin/ActEdit/operatepaginate', //
                            //     cols:operatecols,
                            //     where: form.val('table-search')
                            //     ,
                            //     page: {
                            //         curr: 1 // 可选，重载后初始显示的页码，默认为 1
                            //     },
                            //     data: res
                            //
                            // });
                        });

                    }


                    if (obj.event === 'sendselect') {
                        var searchData = form.val('table-search');

                        searchData.data=senddata;
                        console.log('snda '+senddata[0].status)
                        if (senddata[0].status ) {
                            layer.msg("请重新查询之后在发送...");
                            return false;
                        }
                        //判断roleid是否有重复
                        if (!senddata ) {
                            layer.msg("请重新查询之后在发送...");
                            return false;
                        }
                        var roleIds = senddata.map(function(obj) {
                            return obj.roleId;
                        });
                        var uniqueRoleIds = [...new Set(roleIds)];

                        // 判断是否存在重复的roleId
                        if (roleIds.length !== uniqueRoleIds.length) {
                            layer.msg("存在重复的ID...");
                            return false;
                        }
                        if(!searchData.acttype){

                            layer.msg("角色类型不能为空...");

                            return false;
                        }
                        if(!searchData.actid){
                            layer.msg("角色ID不能为空...");
                            return false;
                        }
                        if(!searchData.data){
                            layer.msg("查询数据为空,请查询之后在执行发送...");
                            return false;
                        }

                        console.log('查询数据xxxx'+senddata)

                        var layerIndex = layer.msg("请求中请耐心等待...",{icon:16,time:999999999});
                        $.post('/admin/ActEdit/send',searchData,function(res){
                            layer.close(layerIndex)
                            //loadTable();// 监听搜索操作
                            //重新加载操作记录
                            // table.reload('currentTableId', {
                            //     url: '/admin/ActEdit/operatepaginate', //
                            //     cols:operatecols,
                            //     where: form.val('table-search')
                            //     ,
                            //     page: {
                            //         curr: 1 // 可选，重载后初始显示的页码，默认为 1
                            //     },
                            //     data: res
                            //
                            // });
                        });

                    }
                });
                form.render()
            });
        }
    })

</script>
</body>
</html>